package com.csun.cmny.provider.controller.message;

import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.domain.OmcFlowMessageGroup;
import com.csun.cmny.provider.model.dto.message.FlowMessageQueryDto;
import com.csun.cmny.provider.model.vo.message.FlowMessageVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcFlowMessageGroupService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-10-28 10:11
 * @Description:
 */
@RestController
@RequestMapping(value = "/flowMessageGroup", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcFlowMessageGroupController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;
    @Resource
    private OmcFlowMessageGroupService omcFlowMessageGroupService;

    /**
     * 绑定
     */
    @PostMapping(value = "/bind")
    public Wrapper bind(@RequestBody OmcFlowMessageGroup omcFlowMessageGroup) {

        Integer result = omcFlowMessageGroupService.bind(omcFlowMessageGroup);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.ok(result);
    }

    /**
     * 解绑
     */
    @PostMapping(value = "/untied")
    public Wrapper untied(@RequestBody OmcFlowMessageGroup omcFlowMessageGroup) {

        Integer result = omcFlowMessageGroupService.untied(omcFlowMessageGroup);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.ok(result);
    }

    /**
     * 修改
     */
    @PostMapping(value = "/edit")
    public Wrapper edit(@RequestBody OmcFlowMessageGroup omcFlowMessageGroup) {

        Integer result = omcFlowMessageGroupService.edit(omcFlowMessageGroup);
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        return WrapMapper.ok(result);
    }

    /**
     * 查询 - 分页
     */
    @PostMapping(value = "/queryListWithPage")
    public Wrapper queryListWithPage(@RequestBody FlowMessageQueryDto flowMessageQueryDto) {

        if (flowMessageQueryDto.getGroupId() == null) {
            flowMessageQueryDto.setGroupId(getLoginAuthDto().getGroupId());
        }

        List<FlowMessageVo> list = omcFlowMessageGroupService.queryListWithPage(flowMessageQueryDto);

        return WrapMapper.ok(new PageInfo<>(list));
    }

}
